package com.h.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.h.blog.common.constant.Constants;
import com.h.blog.common.exception.BlogException;
import com.h.blog.common.unit.BeanTransUnits;
import com.h.blog.mapper.BlogInfoMapper;
import com.h.blog.pojo.dataobject.BlogInfo;
import com.h.blog.pojo.request.AddBlogRequest;
import com.h.blog.pojo.request.UpdateBlogRequest;
import com.h.blog.pojo.response.BlogInfoResponse;
import com.h.blog.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {
    @Autowired
    private BlogInfoMapper blogInfoMapper;

    @Override
    public List<BlogInfoResponse> getList() {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BlogInfo::getDeleteFlag,Constants.BLOG_NORMAL);
        List<BlogInfo> blogInfos = blogInfoMapper.selectList(queryWrapper);

        List<BlogInfoResponse> blogInfoResponse = blogInfos.stream()
                .map(blogInfo -> BeanTransUnits.trans(blogInfo)).collect(Collectors.toList());
        return blogInfoResponse;
    }

    @Override
    public BlogInfoResponse getBlogDetail(Integer blogId) {
        return BeanTransUnits.trans(getBlogInfo(blogId));
    }

    @Override
    public BlogInfo getBlogInfo(Integer blogId) {
        QueryWrapper<BlogInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(BlogInfo::getDeleteFlag,Constants.BLOG_NORMAL)
                .eq(BlogInfo::getId,blogId);
        return blogInfoMapper.selectOne(queryWrapper);
    }

    @Override
    public Boolean add(AddBlogRequest addBlogRequest) {
        BlogInfo blogInfo = BeanTransUnits.trans(addBlogRequest);
        try {
            int result = blogInfoMapper.insert(blogInfo);
            if(result == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("博客添加失败, e {}: " + e);
            throw new BlogException("内部错误，请联系管理员");
        }
    }

    @Override
    public Boolean update(UpdateBlogRequest updateBlogRequest) {
        BlogInfo blogInfo = BeanTransUnits.trans(updateBlogRequest);
        try {
            int result = blogInfoMapper.updateById(blogInfo);
            if(result == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("博客修改失败, e {}: " + e);
            throw new BlogException("内部错误，请联系管理员");
        }
    }

    @Override
    public Boolean delete(Integer blogId) {
        BlogInfo blogInfo = new BlogInfo();
        blogInfo.setId(blogId);
        blogInfo.setDeleteFlag(Constants.BLOG_DELETE);
        try {
            // 逻辑上的删除
            int result = blogInfoMapper.updateById(blogInfo);
            if(result == 1) {
                return true;
            }
            return false;
        }catch (Exception e) {
            log.error("博客删除失败, e {}: " + e);
            throw new BlogException("内部错误，请联系管理员");
        }
    }
}
